/**
 * Called to process data submitted to the spreadsheet from a form.
 */
function onSubmit(e) {
  // Dump to log if in testing org
  if(e.namedValues['Organisation type'][0] == ORG_TYPE_TEST) {
    for(nv in e.namedValues) {
      Logger.log(nv + " : " + e.namedValues[nv]);
    }
  }
  
  // Parse form data
  var booking = parseRequestToObj_(e);
  
  if(booking != null) {
    Logger.log("Processing booking from %s", booking.organisation.name);
    processBooking(booking);
  }
}

/**
 * Parses a string for a given value and returns if the value is contained in the string.
 */
function parseBoolInValue_(inStr, value) {
  if(inStr == null) {
    return false;
  }
  return inStr.indexOf(value) >= 0;
}

/**
 * Converts form date strings into GS dates.
 */
function parseDate_(dateStr) {
  var date = new Date();
  var dParts = dateStr.split("/");
  
  // Date elements
  date.setFullYear(dParts[2]);
  date.setMonth(dParts[1] - 1); // setMonth is 0-based
  date.setDate(dParts[0]);
  
  // Time elements
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);
  date.setMilliseconds(0);
  
  return date;
}

/**
 * Parses the form response.
 */
function parseRequestToObj_(e) {
  
  var strOrgName = e.namedValues['Organisation name'][0];
  var strOrgType = e.namedValues['Organisation type'][0];
  
  var strContactName = e.namedValues['Contact name'][0];
  var strContactTelephone = e.namedValues['Contact telephone number'][0];
  var strContactEmail = e.namedValues['Contact e-mail address'][0];
  
  var strHouseBooking = e.namedValues['House booking'][0];
  var strFieldBooking = e.namedValues['Field booking'][0];
  
  var strEventType = e.namedValues['Event type'][0];
  
  var strArrivalDate = e.namedValues['Arrival date'][0];
  var strDepartureDate = e.namedValues['Departure date'][0];
  var strEventTime = e.namedValues['Event time'][0];
  
  var strParticipants = e.namedValues['Participants'][0];
  var strLeaders = e.namedValues['Leaders'][0];
  
  var strEquipmentHire = e.namedValues['Equipment hire'][0];
  var strTents = e.namedValues['Tent hire'][0];
  
  var strNotes = e.namedValues['Special requirements'][0];
  
  var strEventName = strOrgName + "_" + strArrivalDate;
  
  var obj = {
      "timestamp"       : new Date()
    , "eventName"       : strEventName
    , "organisation"    : {
          "name" : strOrgName
        , "type" : strOrgType
      }
    , "contact"         : {
          "name"      : strContactName
        , "telephone" : strContactTelephone
        , "email"     : strContactEmail
      }
    , "eventType"       : strEventType
    , "eventTime"       : strEventTime
    , "isHouseBooking"  : parseBoolInValue_(strHouseBooking, "Yes")
    , "isFieldBooking"  : parseBoolInValue_(strFieldBooking, "Yes")
    , "arrivalDate"     : parseDate_(strArrivalDate)
    , "departureDate"   : parseDate_(strDepartureDate)
    , "equipment"       : {
          "fullyEquipped" : parseBoolInValue_(strEquipmentHire, "Fully equipped")
        , "tents"         : strTents
      }
    , "participants"    : Number(strParticipants)
    , "leaders"         : Number(strLeaders)
    , "notes"           : strNotes
    , "status"          : "NEW"
    , "docs"            : {}
  };
  
  return obj;
}
